"""借款页面对象，主要练习输入文本和下拉框。"""
import time
from selenium.webdriver.common.by import By
import allure
from base.base import BasePage, BaseHandler


class BorrowPage(BasePage):
    def __init__(self):
        super().__init__()
        self.click1 = By.PARTIAL_LINK_TEXT, '品质理财'
        self.click2 = By.XPATH, '//li[1]/dl/dd/a'
        self.title = By.XPATH, '//input[@ng-model="borrow_loan.name"]'
        self.usage = By.XPATH, '//select[@ng-model="borrow_loan.use"]'
        self.amount = By.XPATH, '//input[@ng-model="borrow_loan.amount"]'
        self.profit = By.XPATH, '//input[@ng-model="borrow_loan.apr"]'
        self.validate = By.XPATH, '//select[@ng-model="borrow_loan.validate"]'
        self.min_amount = By.ID, 'tender_amount_min'
        self.max_amount = By.ID, 'tender_amount_max'
        self.pswd = By.XPATH, '//input[@ng-model="borrow_loan.password"]'
        self.contents = By.XPATH, '//textarea[@ng-model="borrow_loan.contents"]'
        self.vericode = By.XPATH, '//input[@ng-model="borrow_loan.valicode"]'
        self.submit = By.CSS_SELECTOR, '.btn-submit'
        # 提示信息
        self.msg = By.CSS_SELECTOR, '.validation-invalid'

    def click1_elem(self): return self.get_element(self.click1)

    def click2_elem(self): return self.get_element(self.click2)

    def title_elem(self): return self.get_element(self.title)

    def usage_elem(self): return self.get_element(self.usage)

    def amount_elem(self): return self.get_element(self.amount)

    def profit_elem(self): return self.get_element(self.profit)

    def validate_elem(self): return self.get_element(self.validate)

    def min_amount_elem(self): return self.get_element(self.min_amount)

    def max_amount_elem(self): return self.get_element(self.max_amount)

    def pswd_elem(self): return self.get_element(self.pswd)

    def contents_elem(self): return self.get_element(self.contents)

    def vericode_elem(self): return self.get_element(self.vericode)

    def submit_elem(self): return self.get_element(self.submit)

    def msg_elem(self): return self.get_element(self.msg)


class BorrowHandler(BaseHandler):
    def __init__(self):
        self.borrow_page = BorrowPage()

    def click_1(self):
        self.borrow_page.click1_elem().click()

    def click_2(self):
        self.borrow_page.click2_elem().click()

    @allure.step(title="输入借款标题")
    def input_title(self, text):
        self.input_text(self.borrow_page.title_elem(), text)

    @allure.step(title="选择借款用途")
    def select_usage(self, select=1):
        if select: self.select(self.borrow_page.usage_elem(), "其他")

    @allure.step(title="输入借款金额")
    def input_amount(self, num):
        self.input_text(self.borrow_page.amount_elem(), num)

    @allure.step(title="输入年利率")
    def input_profit(self, profit):
        self.input_text(self.borrow_page.profit_elem(), profit)

    @allure.step(title="选择筹标期限")
    def select_validate(self, select=1):
        if select: self.select(self.borrow_page.validate_elem(), "10天")

    @allure.step(title="选择最低投资金额")
    def select_min(self, select=1):
        if select: self.select(self.borrow_page.min_amount_elem(), "50元")

    @allure.step(title="选择最高投资金额")
    def select_max(self, select=1):
        if select: self.select(self.borrow_page.max_amount_elem(), "100元")

    @allure.step(title="输入投资密码，非必须")
    def input_pswd(self, select=0):
        if select: self.input_text(self.borrow_page.pswd_elem(), '8888')

    @allure.step(title="输入借款描述")
    def input_contents(self, select=1):
        if select: self.input_text(self.borrow_page.contents_elem(), '借款描述')

    @allure.step(title="输入验证码")
    def input_verify(self, select=1,verify_code='8888'):
        if select: self.input_text(self.borrow_page.vericode_elem(), verify_code)

    @allure.step(title="点击提交")
    def click_submit(self):
        self.borrow_page.submit_elem().click()

    def get_msg(self):
        return self.borrow_page.msg_elem().text


class BorrowProxy:
    def __init__(self):
        self.borrow_handle = BorrowHandler()

    def borrow(self, title_text, amount, profit, select_li):
        self.borrow_handle.click_1()
        self.borrow_handle.click_2()
        self.borrow_handle.input_title(title_text)
        self.borrow_handle.input_amount(amount)
        self.borrow_handle.input_profit(profit)
        self.borrow_handle.select_usage(select=select_li[0])
        self.borrow_handle.select_validate(select=select_li[1])
        self.borrow_handle.select_min(select=select_li[2])
        self.borrow_handle.select_max(select=select_li[3])
        self.borrow_handle.input_pswd(select=select_li[4])
        self.borrow_handle.input_contents(select=select_li[5])
        self.borrow_handle.input_verify(select=select_li[6])
        self.borrow_handle.click_submit()

    def get_borrow_msg(self):
        time.sleep(1)
        msg = self.borrow_handle.get_msg()
        return msg
